package com.itzhoun.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.itzhoun.listener.ExcelReadListener;
import com.itzhoun.service.ImportService;
import com.itzhoun.util.EasyExcelUtil;
import com.itzhoun.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 周楠
 * @Date 2022/07/14/下午 9:20
 */
@Slf4j
@Service
public class ImportServiceImpl implements ImportService {

    @Override
    public Boolean importFile(MultipartFile file) {
        try {
            ExcelReadListener<UserVO> readListener = new ExcelReadListener<>();
            EasyExcel.read(file.getInputStream(), UserVO.class, readListener).sheet().doRead();
            List<UserVO> list = readListener.getList();
            for (UserVO userVO : list) {
                log.info("每一条对象信息:{}", userVO);
            }
            return true;
        }catch (IOException e){
            log.error("导入异常，异常信息{}", e.getMessage());
        }
        return false;
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        List<UserVO> data = data();
//            EasyExcelFactory.write("fileName.xlsx", UserVO.class).sheet("分数统计").doWrite(data());


        //导出excel
            EasyExcelUtil.writeWeb(response, UserVO.class, data, "导出", "帝业也");
//        EasyUtil.writeExcel(response, data, "fileName", "sheetName", new UserVO() );
    }

    private List<UserVO> data() {
        List<UserVO> list = new ArrayList<>();
        long startTime=System.currentTimeMillis();
        log.info("当前时间{}", LocalDateTime.now());
        for (int i = 0; i < 90000; i++) {
            UserVO data = new UserVO();
            data.setAge(11+ i);
            data.setUsername("周楠"+ i);
            data.setIdCard("1111111233000" +i);
            list.add(data);
        }
        long endTime=System.currentTimeMillis();
        float excTime=(float)(endTime-startTime)/1000;
        log.info("当前前隔时间{}", excTime);
        return list;
    }

    @Override
//    @Async("asyncExecutor")
    public List<UserVO> userList() {
        return data();
    }
}
